package tt.com.datamanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.CremateStove;
import tt.com.common.model.Hall;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.mapper.HallMappers;
import tt.com.datamanager.service.HallService;

import java.util.Date;

@Service
public class HallServiceImpl extends ServiceImpl<HallMappers, Hall> implements HallService {
    @Autowired
    private HallMappers hallMappers;

    @Override
    public PageUtil selectAllHallByPage(PageUtil pageUtil) {
        Page<Hall> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<Hall> hallPage = baseMapper.selectPage(page, null);
        // 设置总条数 自动计算总页数
        pageUtil.setTotalSize((int) hallPage.getTotal());
        pageUtil.setData(hallPage);
        return pageUtil;
    }

    @Override
    public Integer updateHall(Hall hall, String updateUserId) {
        // 设置修改时间
        hall.setLastUpdateTime(new Date());
        // 设置修改操作人id
        hall.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(hall);
    }

    @Override
    public Integer deleteHall(Long id, String deleteUserId) {
        //根据id获取灵堂
        Hall hall = baseMapper.selectById(id);
        if (hall == null){
            //若数据不存在，返回-1
            return -1;
        }
        if (hall.getDataStatus() == 1){
            //判断是否已经被删除过
            return -2;
        }
        //设置删除时间
        hall.setDeleteTime(new Date());
        //设置删除操作人id
        hall.setDeleteUserId(deleteUserId);
        hall.setDataStatus(1);
        baseMapper.updateById(hall);
        return baseMapper.deleteById(hall.getId());
    }

    @Override
    public Integer insertHall(Hall hall) {
        QueryWrapper<Hall> wrapper = new QueryWrapper<>();
        wrapper.eq("id", hall.getId());
        if (baseMapper.selectOne(wrapper) != null){
            // 重复添加返回-1
            return -1;
        }
        // 设置创建时间
        hall.setCreateTime(new Date());
        hall.setDataStatus(0);
        return baseMapper.insert(hall);
    }


}
